package date;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static java.time.temporal.ChronoUnit.DAYS;

/**
 * @Author: xhuan
 * @CreateTime: 2024-07-09
 * @Description: 日期转换
 */
public class DateUtil {
    public static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter formatterYearMonth = DateTimeFormatter.ofPattern("yyyy-MM");


    //region 日期格式转换：date，strTime，localDateTime，localDate

    //region localDateTime转换为str,date,localDate
    /**
     * localDateTime转换为str
     * @param localDateTime
     * @return
     */
    public static String localDateTimeToStrTime(LocalDateTime localDateTime) {
        return localDateTime.format(dateTimeFormatter);
    }
    /**
     * localDateTime转换为Date
     * @param localDateTime
     * @return
     */
    public static Date localDateTimeToDate(LocalDateTime localDateTime) {

        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());

    }

    /**
     * localDateTime转换为localDate
     * @param localDateTime
     * @return
     */
    public static LocalDate localDateTimeToLocalDate(LocalDateTime localDateTime) {
        return localDateTime.toLocalDate();

    }

    //endregion

    //region date转换为str,localDateTime,localDate
    /**
     * date转换为localDateTime
     * @param date
     * @return
     */
    public static LocalDateTime dateToLocalDateTime(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
    }
    /**
     * date转换为字符串
     * @param date
     * @return
     */
    public static String dateToStrTime(Date date) {
        LocalDateTime localDateTime = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        return localDateTime.format(dateTimeFormatter);
    }
    /**
     * date转换为localDate
     * @param date
     * @return
     */
    public static LocalDate dateToLocalDate(Date date) {
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }
    //endregion

    //region str转换为date,localDateTime,localDate
    /**
     * 字符串转换为Date
     * @param dateStr
     * @return
     */
    public static Date strTimeToDate(String dateStr) {
        LocalDateTime localDateTime = LocalDateTime.parse(dateStr, dateTimeFormatter);
        return Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * str转换为localDateTime
     * 字符串yyyy-MM-dd HH:mm:ss格式转换为LocalDateTime类型
     *
     * @param strTime yyyy-MM-dd HH:mm:ss格式
     */
    public static LocalDateTime strTimeToLocalDateTime(String strTime) {
        return LocalDateTime.parse(strTime, dateTimeFormatter);
    }

    /**
     * str转换为localDate
     * 字符串yyyy-MM-dd格式转换为LocalDate类型
     *
     * @param strTime yyyy-MM-dd格式
     */
    public static LocalDate strTimeToLocalDate(String strTime) {
        return LocalDate.parse(strTime, dateFormatter);
    }
    //endregion

    //region localDate转换为date,localDateTime,str
    /**
     * localDate转换为Date
     * @param localDate
     * @return
     */
    public static Date localDateToDate(LocalDate localDate) {
        return Date.from(localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant());
    }

    /**
     * localDate转换为localDateTime
     *
     *
     * @param localDate yyyy-MM-dd格式
     */
    public static LocalDateTime localDateToLocalDateTime(LocalDate localDate) {
        return localDate.atStartOfDay();
    }

    /**
     * localDate转换为str
     * LocalDate类型转换为字符串yyyy-MM-dd格式
     *
     * @param localDate yyyy-MM-dd格式
     */
    public static String localDateToStr(LocalDate localDate) {
        return localDate.format(dateFormatter);
    }
    //endregion
    //endregion


    /**
     * localDateTime获取值，时分秒等
     * @param localDateTime
     */
    public static void getLocalDateTimeValue(LocalDateTime localDateTime) {

        int year = localDateTime.getYear();
        System.out.println("获取年份 = " + year);
        int dayOfYear = localDateTime.getDayOfYear();
        System.out.println("获取一年的第几天 = " + dayOfYear);

        int dayOfMonth = localDateTime.getDayOfMonth();
        System.out.println("获取一个月的第几天 = " + dayOfMonth);
        int monthValue = localDateTime.getMonthValue();
        System.out.println("获取第几个月 = " + monthValue);
        Month month = localDateTime.getMonth();
        System.out.println("获取月份 = " + month);

        // 判断是否是平年
        boolean leap = Year.isLeap(year);
        System.out.println("判断是否是平年 = " + leap);

    }

    /**
     * localDateTime计算，加减
     * @param localDateTime
     */
    public static void localDateTimeCalculate(LocalDateTime localDateTime) {
        LocalDateTime plusYear1 = localDateTime.plus(1, ChronoUnit.YEARS);
        System.out.println("1年后（1）：" + plusYear1);
        LocalDateTime plusYear2 = localDateTime.plusYears(1);
        System.out.println("1年后（2）：" + plusYear2);
        LocalDateTime minusYear1 = localDateTime.minus(1, ChronoUnit.YEARS);
        System.out.println("1年前（1）：" + minusYear1);
        LocalDateTime minusYear2 = localDateTime.minusYears(1);
        System.out.println("1年前（2）：" + minusYear2);

        LocalDateTime withYear = localDateTime.withYear(2015);
        System.out.println("将年份指定到某一年 = " + withYear);
        LocalDateTime withDayOfYear = localDateTime.withDayOfYear(3);
        System.out.println("将天数指定到这一年的第几天 = " + withDayOfYear);
        LocalDateTime withDayOfMonth = localDateTime.withDayOfMonth(3);
        System.out.println("将天数指定到这一月的第几天 = " + withDayOfMonth);
        LocalDateTime localDateTime1 = localDateTime.with(ChronoField.DAY_OF_WEEK, 5);
        System.out.println("当前日期最近的星期几 = " + localDateTime1);

    }

    /**
     * localDateTime日期比较判断
     * @param localDateTime1
     */
    public static void localDateTimeJudge(LocalDateTime localDateTime1,LocalDateTime localDateTime2) {
        boolean equal = localDateTime1.isEqual(localDateTime2);
        System.out.println("localDateTime1 == localDateTime2 " + equal);
        boolean before = localDateTime1.isBefore(localDateTime2);
        System.out.println("localDateTime1 < localDateTime2 " + before);
        boolean after = localDateTime1.isAfter(localDateTime2);
        System.out.println("localDateTime1 > localDateTime2 " + after);
    }

    /**
     * 获取两个时间间隔内的所有日期
     *
     * @return
     */
    public static List<Date> getDayBetweenDates(String start, String end) {

        List<Date> result = new ArrayList<Date>();
//        LocalDate startDate = strTimeToLocalDate(start);
//        LocalDate endDate = strTimeToLocalDate(end);
//        while (!startDate.isAfter(endDate)) {
//            result.add(localDateToDate(startDate));
//            startDate = startDate.plusDays(1L);
//        }

        return result;
    }


    /**
     * 获取两个时间间隔内的天数
     * @param start 开始时间
     * @param end 结束时间
     * @return
     */
    public static long getDayNumBetweenDates(String start, String end) {

        List<Date> result = new ArrayList<Date>();
        LocalDate startDate = strTimeToLocalDate(start);
        LocalDate endDate = strTimeToLocalDate(end);
        long between = DAYS.between(startDate, endDate);
        long between1 = startDate.until(endDate, DAYS);

        return between;
    }
}
